import {
	ref,
	unref
} from 'vue';

const isWindow = (val) => val === window;

const makeDOMRect = (width, height) => ({
	top: 0,
	left: 0,
	right: width,
	bottom: height,
	width,
	height,
});

export const useRect = (elementOrRef) => {
	const element = unref(elementOrRef);
	if (isWindow(element)) {
		const width = element.innerWidth;
		const height = element.innerHeight;
		let rect = makeDOMRect(width, height);
		return rect;
	}

	if (element?.getBoundingClientRect) {
		return element.getBoundingClientRect();
	}

	return makeDOMRect(0, 0);
};